package com.xingdata.jjxc.base;

import java.util.HashMap;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.activity.AbActivity;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.enty.UserEntity;
import com.xingdata.jjxc.http.HttpUtil;
import com.xingdata.jjxc.utils.Constants;
import com.xingdata.jjxc.utils.ExitApplication;
import com.xingdata.jjxc.utils.SP;
import com.xingdata.jjxc.views.Widget;

/**
 * 公共Activity，写出相应的接口。简化了多个extends Activity写不同的繁琐的方法。
 * 定义了一些公共的接口。protected abstract String getTitleText();
 * @author ysc
 *
 */
public abstract class Base1Activity extends Activity {
	
	final static String TAG = "Base1Activity";

	/**
	 * 主标题
	 */
	protected TextView title_tv;

	/**
	 * 返回
	 */
	protected View back_iv;

	/**
	 * 【返回键操作】按下返回键之后，计算下一次按下返回键的时间差值
	 */
	private long exitTime = 0;


	/**
	 * 【HTTP请求】声明Http请求实例，并声明参数实例
	 */
	protected HttpUtil mHttpUtil = null;
	protected HashMap<String, String> paramsMap=null;
	protected HashMap<String, Object> fileMap=null;

	// /**
	// *
	// */
	// protected UserEntity user;
	// protected String[] info;

	protected GestureDetector mGestureDetector;
	protected ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, getClass().getSimpleName());//识别每个继承Base1Activity的类，让我们知道当前界面的类是哪个类。看到界面就能从logcat中知道是哪个类
		// ExitApplication.getInstance().addActivity(this);
		Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/PingFang-SC-Regular.ttf");
		// 获取Http工具类
		mHttpUtil = HttpUtil.getInstance(this);
		// mAbHttpUtil.setDebug(true);
		// mAbHttpUtil.setTimeout(15000);

		// user = SP.getUserInfo(this);
		// info = SP.getMobileAndPassword(this);

		initDateBeforSetContentView();
		setContentView(getContentViewId());

		if (getTitleText() != null) {
			title_tv = (TextView) findViewById(R.id.title);
			title_tv.setTypeface(font);
			if (title_tv != null) {
				title_tv.setText(getTitleText());

			}
			back_iv = findViewById(R.id.title_back);
			if (back_iv != null) {

				back_iv.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						finish();
					}
				});
			}

		}


		initViews();

	}

	/**
	 * 声明所在的页面的资源文件xml - (R.layout.xxx);
	 * 
	 * @return
	 */
	protected abstract int getContentViewId();

	/**
	 * 声明所在页面的title名称
	 * 
	 * @return
	 */
	protected abstract String getTitleText();

	/**
	 * 此方法用于页面传值，在控件声明之前调用, 比如:
	 * 
	 * Bundle b = getIntent().getExtras(); String s = b.getString("s");
	 * 
	 */
	protected void initDateBeforSetContentView() {

	}

	/**
	 * 控件都在此方法中声明, 比如:
	 * 
	 * Button b = (Button) findViewById(R.id.button);
	 * 
	 */
	protected abstract void initViews();

	/**
	 * 判断网络连接状态
	 * 
	 * @param context
	 * @return 状态
	 */
	protected boolean isNetworkConnected() {

		ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

		cwjManager.getActiveNetworkInfo();
		boolean netSataus = true;
		if (cwjManager.getActiveNetworkInfo() == null) {

			netSataus = false;
		}
		return netSataus;
	}

	

	protected ProgressDialog showProgressDialog() {
		return showProgressDialog(null, "正在加载...");

	}

	/**
	 * 显示 ProgressDialog
	 */
	protected ProgressDialog showProgressDialog(String title, String message) {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
		if (title == null) {
			title = "提示";
		}
		progressDialog = ProgressDialog.show(this, title, message);
		progressDialog.setCancelable(true);
		progressDialog
				.setOnCancelListener(new DialogInterface.OnCancelListener() {

					@Override
					public void onCancel(DialogInterface dialog) {
						dialog.dismiss();
					}
				});
		return progressDialog; 
	}

	/**
	 * 显示 ProgressDialog
	 */
	protected ProgressDialog showProgressDialog(String message) {
		return showProgressDialog(null, message);
	}

	/**
	 * 更新ProgressDialog提示消息
	 * 
	 * @param progressDialog
	 * @param title
	 * @param message
	 * @return
	 */
	protected ProgressDialog updataProgressDialog(String strTitle,
			String strMessage) {
		String title = "";
		String message = "";
		if (progressDialog != null && progressDialog.isShowing()) {
			if (strTitle != null) {
				title = strTitle;
			}
			if (strMessage != null) {
				message = strMessage;
			}
			progressDialog.setTitle(title);
			progressDialog.setMessage(message);
		} else {
			showProgressDialog(title, message);
		}
		return progressDialog;

	}

	/**
	 * 隐藏ProgressDialog
	 */
	protected void dismissProgressDialog() {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}

	/**
	 * 隐藏ProgressDialog
	 */
	protected void removeProgressDialog() {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}

	protected void showToast(String s) {
		Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
	}

	protected void showToast(int i) {
		String staToast = getResources().getString(i).toString();
		Toast.makeText(this, staToast, Toast.LENGTH_SHORT).show();
	}

}
